package jee.boot.common.basic;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jee.boot.common.utils.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页类
 *
 * @param <T>
 * @author zhilei.wang
 */
public class Page<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int pageNo = 1; // 当前页码
    private int pageSize = -1;
    private long count;// 总记录数
    private List<T> list = new ArrayList<T>();
    private String orderBy; // 排序
    private String orderSort;// 排序类型 desc asc(默认)
    private String defaultAlias = "a";// 默认别名
    private boolean turnUnderline = true;// 是否对orderBy进行转换
    // true: userName->a.user_name;
    // false:userName->a.userName

    public Page() {
        this.pageSize = -1;
    }

    /**
     * 构造方法
     *
     * @param pageNo   当前页码
     * @param pageSize 分页大小
     */
    public Page(int pageNo, int pageSize) {
        this(pageNo, pageSize, 0);
    }

    /**
     * 构造方法
     *
     * @param pageNo   当前页码
     * @param pageSize 分页大小
     * @param count    数据条数
     */
    public Page(int pageNo, int pageSize, long count) {
        this(pageNo, pageSize, count, new ArrayList<T>());
    }

    /**
     * 构造方法
     *
     * @param pageNo   当前页码
     * @param pageSize 分页大小
     * @param count    数据条数
     * @param list     本页数据对象列表
     */
    public Page(int pageNo, int pageSize, long count, List<T> list) {
        this.setCount(count);
        this.setPageNo(pageNo);
        this.pageSize = pageSize;
        this.list = list;
    }

    /**
     * 获取设置总数
     *
     * @return
     */
    public long getCount() {
        return count;
    }

    /**
     * 设置数据总数
     *
     * @param count
     */
    public void setCount(long count) {
        this.count = count;
        if (pageSize >= count) {
            pageNo = 1;
        }
    }

    /**
     * 获取当前页码
     *
     * @return
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 设置当前页码
     *
     * @param pageNo
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    /**
     * 获取页面大小
     *
     * @return
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置页面大小
     *
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取本页数据对象列表
     *
     * @return List<T>
     */
    public List<T> getList() {
        return list;
    }

    /**
     * 设置本页数据对象列表
     *
     * @param list
     */
    public Page<T> setList(List<T> list) {
        this.list = list;
        return this;
    }

    public String getOrderBy() {
        return orderBy;
    }

    /**
     * 解析页面传入的排序属性,废弃,已转移至entity中
     *
     * @return
     */
    @Deprecated
    @JsonIgnore
    public String getOrderBySql() {
        String orderBy = getOrderBy();
        String orderSort = getOrderSort();

        if (StringUtils.isNotBlank(orderBy)) {
            String[] orderList = StringUtils.split(orderBy, ",");
            String[] sortList = null;
            if (StringUtils.isNotBlank(orderSort)) {
                sortList = StringUtils.split(orderSort, ",");
            }
            StringBuilder sql = new StringBuilder();
            for (int i = 0; i < orderList.length; i++) {
                String alias = getDefaultAlias();
                String order = orderList[i];
                String sort = "asc";
                if (sortList != null) {
                    try {
                        //排序方法按着属性的位置在orderSort中获取,如果获取失败,则使用asc默认排序
                        sort = sortList[i];
                    } catch (Exception e) {

                    }
                }
                // 设置表别名
                if (order.indexOf(".") > 0) {
                    alias = order.substring(0, order.lastIndexOf("."));
                }
                String field;
                if (isTurnUnderline()) {
                    // 转成下换线形式
                    field = StringUtils.toUnderScoreCase(order.substring(order.lastIndexOf(".") + 1));
                } else {
                    field = order.substring(order.lastIndexOf(".") + 1);
                }
                sql.append(" "+(StringUtils.isBlank(alias) ? "" : alias+".")).append(field).append(" ").append(sort).append(" ,");
            }
            return StringUtils.removeEnd(sql.toString(),",");

        }
        return null;
    }

    /**
     * 设置查询排序
     */
    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getOrderSort() {
        return orderSort;
    }

    public void setOrderSort(String orderSort) {
        this.orderSort = orderSort;
    }

    public void setTurnUnderline(boolean turnUnderline) {
        this.turnUnderline = turnUnderline;
    }

    public boolean isTurnUnderline() {
        return turnUnderline;
    }

    public String getDefaultAlias() {
        return defaultAlias;
    }

    public void setDefaultAlias(String defaultAlias) {
        this.defaultAlias = defaultAlias;
    }

    /**
     * 获取 Hibernate FirstResult
     */
    @JsonIgnore
    public int getFirstResult() {
        int firstResult = (getPageNo() - 1) * getPageSize();
        if (firstResult >= getCount()) {
            firstResult = 0;
        }
        return firstResult;
    }

    /**
     * 获取 Hibernate MaxResults
     */
    @JsonIgnore
    public int getMaxResults() {
        return getPageSize();
    }

    public static void main(String[] args) {
        Page page=new Page();
        page.setOrderBy("userName,company.userCode");
        page.setOrderSort("desc,desc");
        page.setTurnUnderline(false);
        System.out.println(page.getOrderBySql());
    }
}
